<?php
namespace app\common\library;

use fds\FdsSdk;

/**
 * 支付demo
 */
class Pay
{
    /**
     * 初始化函数
     *
     */
    public function __construct()
    {
        date_default_timezone_set('PRC');           // 设置时区

        // 应用编号，需修改
        // FdsSdk::setAppId("6633965341557002240");
        FdsSdk::setAppId("7348984221107961857");

        // FdsSdk::setServerUrl("http://47.105.146.241:9510/service/soa");
        FdsSdk::setServerUrl("https://moneyconnect.lakala.com/service/soa");
        FdsSdk::setApiVersion("3.0");

        $certPath = EXTEND_PATH . 'fds' . DS . 'cert' . DS;

        // $DSAPublicKeyPath = $certPath . 'cloud_dsa_public.pem';                       // 钱帐通的DSA公钥文件所在目录，需修改
        $DSAPublicKeyPath = $certPath . 'cloud_dsa_public.cer';                       // 钱帐通的DSA公钥文件所在目录，需修改
        $DSAPrivateKeyPath = $certPath . 'dsa_private_key.pfx';                        // 应用自己的DSA私钥文件所在目录，需修改
        $DSAPrivateKeyPass = 'atcrahkij7veqdja';                                       // 应用自己的DSA私钥密码，需修改
        FdsSdk::setDSAKey($DSAPublicKeyPath, $DSAPrivateKeyPath, $DSAPrivateKeyPass);

        // $RSAPublicKeyPath = $certPath . 'cloud_rsa_public.pem';                       // 钱帐通的RSA公钥文件所在目录，需修改
        $RSAPublicKeyPath = $certPath . 'cloud_rsa_public.cer';                       // 钱帐通的RSA公钥文件所在目录，需修改
        $RSAPrivateKeyPath = $certPath . 'rsa_private_key.pfx';                        // 应用自己的RSA私钥文件所在目录，需修改
        $RSAPrivateKeyPass = '2t1fyyq47ekq6ne4';                                       // 应用自己的RSA私钥密码，需修改
        FdsSdk::setRSAKey($RSAPublicKeyPath, $RSAPrivateKeyPath, $RSAPrivateKeyPass);
    }
    
    /**
     * 1.3消费接口
     *
     */
    public function consume($data)
    {
        ///// 组装请求参数


        $result = FdsSdk::getService()->createCom($data, 'order.consume.request');
        // dump($result); exit();

        //// 返回成功跳转
//        if (isset($result['status']) && $result['status'] == 'OK') {
//            header("Location: " . $result['result']['pay_info']['counter_url']);
//        } else {
//            $this->error($result);
//        }
        return $result;
    }


    /**
     * 1.15分账接口
     *
     */
    public function split()
    {
        ///// 组装请求参数
        $data = [
            'member_no'    => 6806043394058620928,                         // 卖家标识
            'account_type' => '1000',                                      // 账户类型编码
            'out_order_no' => '7272915734750400512',                       // 订单号
            'amount'       => 1,                                           // 订单金额（单位：分）
        ];

        ///// 分账规则
        $data['split_rule_data'] = ['split_list' => [
            ['member_no' => 6806043394058620928, 'amount' => 1],
        ]];

        // dump($data);exit();

        $result = FdsSdk::getService()->createCom($data, 'order.balance.split');
        return $result;
    }

    public function consume_pay($data){
        $result = FdsSdk::getService()->createCom($data, 'order.consume.pay.request');
        return $result;
    }

    /**
     * 1.6 退款
     *
     */
    public function refund()
    {
        ///// 组装请求参数
        $data = [
            'order_no'       => 7272915734750400512,                 // 原订单（需要退款的订单编号）
            'out_request_no' => '24121211012001101011001705598',     // 应用平台请求号（对应消费下单时返回的字段pay_order_no）
            'pay_seq_no'     => '7272915734872035328',               // 支付流水号
            'refund_amount'  => 10,                                  // 退款金额
        ];

        $data['refund_rule'] = [[                                        // 退款规则
            'member_no'       => 6806043394058620928,
            'account_type_no' => '1000',
            'amount'          => 10,
        ]];

        $result = FdsSdk::getService()->createCom($data, 'order.consume.refund');
        return $result;
    }


    /**
     * 1.2 提现
     *
     */
    public function withdraw()
    {
        ///// 组装请求参数
        $data = [
            'member_no'     => 6806043394058620928,                         // 卖家标识
            'out_order_no'  => 'ww' . date('YmdHi'),                         // 应用平台订单号
            'account_type'  => '1000',
            'amount'        => 10,                                          // 提现金额（含平台手续费） 单位：分
            'billing_fee'   => 1,                                           // 	平台手续费      单位：分
            'bank_card_id'  => '6806043785454292992',                       // 银行卡的编号
            'withdraw_type' => 'D0',                                        // 提现方式
            'back_url'      => 'http://fish.fa15.com/',                     // 后台通知地址
        ];

        $result = FdsSdk::getService()->createCom($data, 'order.withdraw.request');
        return $result;
    }


    /**
     * 2.1开户进阶接口
     *
     */
    public function addUser()
    {
        ///// 组装请求参数
        $data = [
            'out_member_no'   => 680604339405862092,                         // 卖家标识
            'open_part'       => 1,                                           // 开通角色类型
            'back_url'        => 'http://fish.fa15.com/',                     // 后台通知地址
            'member_category' => 0,                                           // 会员类别
            'member_role'     => 'R003',                                      // 角色编号
            'member_type'     => 2,                                           // 会员类型
        ];

        $result = FdsSdk::getService()->createCom($data, 'member.open.part.page.url.get');
        return $result;
    }


    /**
     * 2.5 查询会员信息
     *
     */
    public function findUser()
    {
        $result = FdsSdk::getService()->createCom(['member_no' => 6806043394058620928], 'member.info.query');
        return $result;
    }


    /**
     * 2.7 查询绑定银行卡列表
     *
     */
    public function findBank()
    {
        $result = FdsSdk::getService()->createCom(['member_no' => 6806043394058620928], 'member.bank.card.bind.query');
        return $result;
    }


    /**
     * 3.1 查询余额
     *
     */
    public function findBalance()
    {
        // $result                 = FdsSdk::getService()->createCom(['member_no' => 6633965347487748096, 'account_type' => '1000'], 'account.balance.get');
        $result = FdsSdk::getService()->createCom(['member_no' => 6806043394058620928, 'account_type' => '1000'], 'account.balance.get');
        return $result;
    }


    public function demo()
    {
        // rsa加密方法
        // FdsSdk::getService()->rsaEncrypt("332624199001011111");

        //外部会员标识，需修改
        $result = FdsSdk::getService()->createCom(['out_member_no' => 'member0001'], 'member.person.create');
        return $result;
    }

    public function member_merchant(){
        $result = FdsSdk::getService()->createCom(['out_member_no' => '6806043394058620928'], 'member.merchant.query');
        return $result;
    }

}
